const { ccclass, property } = cc._decorator;

import WXSdk from "./WX_SDK";
import ZJTD_SDK from "./ZJTD_SDK";

export enum API_TYPE {
    NONE = 0,   //默认
    VX = 1,     //微信
    TT = 2,     //头条
}

export let ApiType: number = API_TYPE.VX;
@ccclass
export default class SDKUtil {

    protected static _instance: SDKUtil = null;
    public static get Instance(): SDKUtil {
        if (SDKUtil._instance == null) {
            SDKUtil._instance = new SDKUtil();
        }
        return SDKUtil._instance;
    }


    init() {
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.init();
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.init();
        }
    }

    init2() {
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.init2();
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.init2();
        }
    }
    // 渠道登录
    login(cb?) {
        console.log("请求登录  ApiType:", ApiType, cc.sys.platform === cc.sys.WECHAT_GAME, !window["tt"]);
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.Login(cb);
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.getWeiXinInfo(cb);
        }
    }

    postData(score, now) {
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.postDataToWX(score, now);
        }
        else if (ApiType == API_TYPE.TT && window["tt"]) {
            ZJTD_SDK.Instance.postDataToWX(score, now)
        }
    }

    //显示底部广告
    AdsShow(number) {
        console.log("AdsShow", number);
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.showBannerAd(number);
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.showBannerAd(number);
        }
    }

    //隐藏底部广告
    AdsHide() {
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.hideBannerAds();
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.hideBannerAds();
        }
    }

    //分享
    ShareApp(i = 0, cb?, fb?) {
        console.log("ShareApp");
        if (ApiType == API_TYPE.VX && cc.sys.platform === cc.sys.WECHAT_GAME) {
            WXSdk.Instance.shareAppMessage(i, cb, fb);
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.shareAppMessage(i, cb, fb);
        }
    }
    //分享到群
    ShareeToGroup(cb = null) {
        console.log("ShareApp");
        if (ApiType == API_TYPE.VX && cc.sys.platform === cc.sys.WECHAT_GAME) {
            WXSdk.Instance.shareToGroupAppMessage(cb);
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.shareAppMessage(cb);
        }
    }

    //显示插屏广告
    InterstitialAd() {
        console.log("InterstitialAdShow");
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.showInterstitialAd();
        }
        else if (ApiType == API_TYPE.TT) {
            //ZJTD_SDK.Instance.showInterstitialAd();
        }
    }

    //显示视频广告
    VideoAdsShow(number, cb?, fb?) {
        console.log("VideoAdsShow", number);
        if (ApiType == API_TYPE.VX) {
            WXSdk.Instance.showRewardedVideoAd(number, cb, fb);
        }
        else if (ApiType == API_TYPE.TT) {
            ZJTD_SDK.Instance.showRewardedVideoAd(number, cb, fb);
        }
    }

    TTRecorderCheck(state) {
        console.log("录屏");
        if (ApiType == API_TYPE.TT) ZJTD_SDK.Instance.TTRecorderCheck(state);
    }

    TTShareVideo(cb?) {
        if (ApiType == API_TYPE.TT) ZJTD_SDK.Instance.TTShareVideo(cb);
    }

    TTShareVideoCheckTime(cb, fb) {
        //大于15秒才分享
        if (ZJTD_SDK.Instance.getVideoTime() >= 15000) {
            SDKUtil.Instance.TTShareVideo((isSuccess) => {
                if (isSuccess === true) {
                    cb && cb();
                    return;
                }
                else if (isSuccess === "faile") {
                    SDKUtil.Instance.VideoAdsShow(0, cb, fb);
                    return;
                }
                else {
                    fb && fb();
                    return;
                }
            });
        }
        else {
            SDKUtil.Instance.VideoAdsShow(0, cb, fb);
        }
    }


}